package com.rizzo.analytics.domain.notification;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

import java.io.Serializable;
import java.util.List;

public class Notifications implements Serializable {

    private static final long serialVersionUID = 22L;

    private List<NotifiedService> notifiedServices;

    public Notifications() {
    }

    public Notifications(List<NotifiedService> notifiedServices) {
        this.notifiedServices = notifiedServices;
    }

    public void addNotifiedService(NotifiedService notifiedService) {
        if (notifiedServices == null) {
            notifiedServices = Lists.newArrayList();
        }
        notifiedServices.add(notifiedService);
    }

    public NotifiedService getNotifiedService(final String service) {
        Predicate<NotifiedService> isService = new Predicate<NotifiedService>() {
            @Override
            public boolean apply(NotifiedService notifiedService) {
                return (notifiedService.getService().equals(service));
            }
        };
        Iterable<NotifiedService> filteredService = Iterables.filter(getNotifiedServices(), isService);
        return Iterables.getFirst(filteredService, null);
    }

    public List<NotifiedService> getNotifiedServices() {
        if (notifiedServices == null) {
            notifiedServices = Lists.newArrayList();
        }
        return notifiedServices;
    }

    public void setNotifiedServices(List<NotifiedService> notifiedServices) {
        this.notifiedServices = notifiedServices;
    }

    @Override
    public String toString() {
        return "Notifications{" +
                "notifiedServices=" + notifiedServices +
                '}';
    }
}
